package com.bykj.taketurns.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bykj.taketurns.R;
import com.bykj.taketurns.utils.Constant;
import com.bykj.taketurns.utils.ScreenBitmapUtils;

public class WriteScreenActivity extends BaseActivity implements View.OnTouchListener{


    private ImageView mWrscTbBackImg;
    private ImageView mWrscTbNextImg;
    private TextView mWrscTbTnTv;
    private EditText mWrscConEt;
    private ScrollView mWrscConScv;
    private int mTouchSlop;

    @Override
    public void initView(Bundle savedInstanceState) {
        ViewConfiguration configuration = ViewConfiguration.get(this);
        // 获取TouchSlop值
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
        //初始化控件
        mWrscTbBackImg = (ImageView) setViewById(R.id.wrsc_tb_back_img);
        mWrscTbNextImg = (ImageView) setViewById(R.id.wrsc_tb_next_img);
        mWrscTbTnTv = (TextView) setViewById(R.id.wrsc_tb_tn_tv);
        mWrscConEt = (EditText) setViewById(R.id.wrsc_con_et);
        mWrscConScv = (ScrollView) setViewById(R.id.wrsc_con_scv);
        //设置点击事件监听
        mWrscTbBackImg.setOnClickListener(this);
        mWrscTbNextImg.setOnClickListener(this);
        mWrscConScv.setOnTouchListener(this);

    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getParentView() {
        return R.layout.activity_write_screen;
    }

    @Override
    public void initToolBar() {

    }

    @Override
    protected void postData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.wrsc_tb_back_img:
                //销毁本页面
                finish();
                break;
            case R.id.wrsc_tb_next_img:
                //保存本页面截图
                String filename = ScreenBitmapUtils.savePic(ScreenBitmapUtils.getBitmapByView(mWrscConScv));
                //进行文字发布
                Intent intent=new Intent(WriteScreenActivity.this,EditVideoActivity.class);
                intent.putExtra(Constant.EXTRA_FILE_NAME,filename);
                startActivity(intent);
                overridePendingTransition(R.anim.tran_in,R.anim.tran_out);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    private float mInitialMotionX;
    private float mInitialMotionY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                final float x = event.getX();
                final float y = event.getY();
                mInitialMotionX = x;
                mInitialMotionY = y;
                Log.d("zzz","downper");

                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                final float upX = event.getX();
                final float upY= event.getY();
                final float dx = upX - mInitialMotionX;
                final float dy = upY - mInitialMotionY;
                Log.d("zzz","open");
                if(Math.abs(dx)>mTouchSlop&&dx<0){
                    if(Math.abs(dx)>Math.abs(dy)) {
                        finish();
                        overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
                        Log.d("zzz","upper");
                        return true;
                    }else {
                        return false;
                    }
                }


        }
        return true;
    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        onTouchEvent(motionEvent);
        return true;
    }
}
